package com.faithnmn.utils.weather;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import android.util.Log;

import com.faithnmn.utils.weather.model.Location;
import com.faithnmn.utils.weather.model.Weather;

public class LocationParser {


    public static Location parse(InputStream inputStream) throws Exception {
    	Location location = new Location();
        SAXReader xmlReader = createXmlReader();
        Document doc = xmlReader.read( inputStream );
        Element root = doc.getRootElement();
		Iterator itr = root.elements().iterator();  
		Element element = null;
		while (itr.hasNext()) {  
		    Element debel = (Element) itr.next();
		     System.out.println(debel.getName());    // node_name_1
		    if (debel.getName().equals("place")) { 
		        Iterator itrd = debel.elementIterator();
		        while (itrd.hasNext())
		        {
		            Element child = (Element) itrd.next();
		            String name = child.getName();  // node_name_2
		            if(name.equalsIgnoreCase("woeid")){
		            	location.woeid = child.getText();
		            }
		            else if(name.equalsIgnoreCase("name")){
		            	location.currentLocation = child.getText();
		            }else if(name.equalsIgnoreCase("admin1")){
		            	location.city = child.getText();
		            }else if(name.equalsIgnoreCase("admin2")){
		            	location.district = child.getText();
		            }else if(name.equalsIgnoreCase("country")){
		            	location.country = child.getText();
		            }else if(name.equalsIgnoreCase("centroid")){
		            	Iterator itrdLocation = child.elementIterator();
		            	while (itrdLocation.hasNext())
				        {
		            		Element locationChild = (Element) itrdLocation.next();
		            		if(locationChild.getName().equalsIgnoreCase("latitude")){
		            			location.latitude = locationChild.getText();
		            		}else if(locationChild.getName().equalsIgnoreCase("longitude")){
		            			location.longitude = locationChild.getText();
		            		}
				        }
		            }
		        }
		   }
		}
        //
        doc = null;
        if(inputStream != null){
        	try {
        		inputStream.close();
			} catch (IOException e) {
			}
        }
        xmlReader = null;
        return location;
    }

    private static SAXReader createXmlReader() {
        Map<String,String> uris = new HashMap<String,String>();
        uris.put( "y", "http://xml.weather.yahoo.com/ns/rss/1.0" );

        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs( uris );

        SAXReader xmlReader = new SAXReader();
        xmlReader.setDocumentFactory( factory );
        return xmlReader;
    }
}
